﻿CKEDITOR.plugins.add('workflow', {
    requires: ['dialog'],
    init: function (editor) {
        var lang = editor.lang;

        editor.addCss(
			'img.cke_hidden' +
			'{' +
				'background-image: url(' + CKEDITOR.getUrl(this.path + 'images/hiddenfield.gif') + ');' +
				'background-position: center center;' +
				'background-repeat: no-repeat;' +
				'border: 1px solid #a9a9a9;' +
				'width: 16px !important;' +
				'height: 16px !important;' +
			'}');

        /*添加命令,工具栏，弹出框*/
        var addButtonCommand = function (buttonName, commandName, dialogFile, iconClass) {
            editor.addCommand(commandName, new CKEDITOR.dialogCommand(commandName));
            editor.ui.addButton(buttonName,
			{
			    label: lang.common[buttonName.charAt(0).toLowerCase() + buttonName.slice(1)],
			    command: commandName,
			    className: iconClass
			});
            CKEDITOR.dialog.add(commandName, dialogFile);
        };

        var dialogPath = this.path + 'dialogs/';
        addButtonCommand('wftext', 'wftext', dialogPath + 'wftext.js', 'cke_button_textfield');
        addButtonCommand('wftextarea', 'wftextarea', dialogPath + 'wftextarea.js', 'cke_button_textarea');
        addButtonCommand('wfselect', 'wfselect', dialogPath + 'wfselect.js', 'cke_button_select');
        addButtonCommand('wfcheckbox', 'wfcheckbox', dialogPath + 'wfcheckbox.js', 'cke_button_checkbox');
        addButtonCommand('wfradio', 'wfradio', dialogPath + 'wfradio.js', 'cke_button_radio');
        addButtonCommand('wfdate', 'wfdate', dialogPath + 'wfdate.js', 'cke_button_date');
        addButtonCommand('wfhtml', 'wfhtml', dialogPath + 'wfhtml.js', 'cke_button_html');
        addButtonCommand('wfhidden', 'wfhidden', dialogPath + 'wfhidden.js', 'cke_button_hiddenfield');

        /*添加右键菜单*/
        if (editor.addMenuItems) {
            editor.addMenuItems(
			{
			    wftext: {
			        label: "单行输入属性",
			        command: 'wftext',
			        group: 'wftext'
			    },
			    wftextarea: {
			        label: "多行输入属性",
			        command: 'wftextarea',
			        group: 'wftextarea'
			    },
			    wfselect: {
			        label: "下拉选择框属性",
			        command: 'wfselect',
			        group: 'wfselect'
			    },
			    wfcheckbox: {
			        label: "多选框属性",
			        command: 'wfcheckbox',
			        group: 'wfcheckbox'
			    },
			    wfradio: {
			        label: "单选框属性",
			        command: 'wfradio',
			        group: 'wfradio'
			    },
			    wfdate: {
			        label: "日期控件属性",
			        command: 'wfdate',
			        group: 'wfdate'
			    },
			    wfhtml: {
			        label: "复杂html",
			        command: 'wfhtml',
			        group: 'wfhtml'
			    },
			    wfhidden: {
			        label: "隐藏值",
			        command: 'wfhidden',
			        group: 'wfhidden'
			    }
			});
        }

        if (editor.contextMenu) {
            editor.contextMenu.addListener(function (element, selection)  //function to be run when context menu is displayed
            {
                if (element && element.is('input')) {
                    switch (element.getAttribute('itemtype')) {
                        case '0': return { wftext: CKEDITOR.TRISTATE_ON }; break;
                        case '4': return { wfcheckbox: CKEDITOR.TRISTATE_ON }; break;
                        case '5': return { wfradio: CKEDITOR.TRISTATE_ON }; break;
                        case '6': return { wfdate: CKEDITOR.TRISTATE_ON }; break;
                        default: return null; break;
                    }
                }
                else if (element && element.is('textarea') && element.getAttribute('itemtype') == 1) {
                    return { wftextarea: CKEDITOR.TRISTATE_OFF };
                }
                else if (element && element.is('select') && element.getAttribute('itemtype') == 2) {
                    return { wfselect: CKEDITOR.TRISTATE_OFF };
                }
                else if (element && element.is('div') && element.getAttribute('itemtype') == 9) {
                    return { wfhtml: CKEDITOR.TRISTATE_OFF };
                }
                else if (element && element.is('img') && element.data('cke-real-element-type') == 'wfhidden') {
                    return { wfhidden: CKEDITOR.TRISTATE_OFF };
                }
                else {
                    return null;
                }
            });
        }

        editor.on('doubleclick', function (evt) {

            var element = evt.data.element;

            if (element && element.is('input')) {
                switch (element.getAttribute('itemtype')) {
                    case '0': evt.data.dialog = 'wftext'; break;
                    case '4': evt.data.dialog = 'wfcheckbox'; break;
                    case '5': evt.data.dialog = 'wfradio'; break;
                    case '6': evt.data.dialog = 'wfdate'; break;
                    default: break;
                }
            }
            else if (element && element.is('textarea') && element.getAttribute('itemtype') == 1) {
                evt.data.dialog = 'wftextarea';
            }
            else if (element && element.is('select') && element.getAttribute('itemtype') == 2) {
                evt.data.dialog = 'wfselect';
            }
            else if (element && element.is('div') && element.getAttribute('itemtype') == 9) {
                evt.data.dialog = 'wfhtml';
            }
            else if (element && element.is('img') && element.data('cke-real-element-type') == 'wfhidden') {
                evt.data.dialog = 'wfhidden';
            }
        });
    }
});